﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using YTE.Control;
using 右下角弹窗.DiagShow;

namespace LD.BC
{
    public partial class AxisPoint : FrmBase
    {
        private List<Form> tempForms;
        public AxisPoint()
        {
            InitializeComponent();
            this.flowLayout.Dock = DockStyle.Fill;
            this.Shown += AxisPoint_Shown;
        }

        private void AxisPoint_Shown(object sender, EventArgs e)
        {
            tempForms = new List<Form>();
            Init();
        }

        private void Init()
        {
            //foreach (var item in BcFactory.AxisParamList)
            //{
            //    AxisMove move = new AxisMove();
            //    move.TopLevel = false;
            //    move.AxisId = (ushort)item.AxisId;
            //    move.AxisName = item.Name;
            //    move.Padding = new Padding(5, 5, 5, 5);
            //    flowLayout.Controls.Add(move);
            //    move.Show();

            //    tempForms.Add(move);

            //    DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
            //    column.Name = item.Name + "";
            //    column.HeaderText = item.Name;
            //    this.dgv_data.Columns.Add(column);
            //}
            //foreach (var item in BcFactory.AxisPointList)
            //{
            //    int index = this.dgv_data.Rows.Add(item.Name, "示教", "移动", item.Point);
            //}
        }
        public void New()
        {
            int index = this.dgv_data.Rows.Add();
            DataGridViewButtonCell btnCell_move = this.dgv_data["Column_Move", index] as DataGridViewButtonCell;
            btnCell_move.Value = "移动";
            DataGridViewButtonCell btnCell_shijiao = this.dgv_data["Column_ShiJiao", index] as DataGridViewButtonCell;
            btnCell_shijiao.Value = "示教";
        }
        public void Save()
        {
            List<TB_AxisPoint> axisPoints = new List<TB_AxisPoint>();
            foreach (DataGridViewRow item in this.dgv_data.Rows)
            {
                string point = item.Cells[1].Value == null ? "" : item.Cells[1].Value.ToString();

                axisPoints.Add(new TB_AxisPoint()
                {
                    Name = item.Cells[1].Value.ToString(),
                    //PartNoId = BcFactory.PartNoId,
                    Point = point,
                });
            }

            SaveDB(axisPoints);
        }
        private void SaveDB(List<TB_AxisPoint> axisPoints)
        {
            //AxisPointDal point = new AxisPointDal();
            ////point.Delete(BcFactory.PartNoId);
            //int result = point.Insert(axisPoints);
            //Dialog dialog = new Dialog();
            //if (result > 0)
            //    dialog.Success("保存完成");
            //else
            //    dialog.Warning("保存失败");
            //BcFactory.AxisPointList = point.GetList(BcFactory.PartNoId);
        }
        private void dgv_data_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int row = e.RowIndex;

            //示教
            if (e.ColumnIndex == 2)
            {
                FrmAxisPointEdit pointEdit = new FrmAxisPointEdit();
                pointEdit.ShowDialog();
                if (pointEdit.Tag != null)
                {
                    this.dgv_data["points", row].Value = pointEdit.Tag;
                }
            }
            //移动
            if (e.ColumnIndex == 3)
            {
                var point = this.dgv_data[e.ColumnIndex, e.RowIndex].Value;
                Dictionary<int, double> points = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<int, double>>(point.ToString());

                foreach (var item in points)
                {
                    BcFactory.Instance.Control.Move_Location((ushort)item.Key, item.Value);
                }
            }
        }

        private void dgv_data_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            Console.WriteLine("数据格式检查");
            if (e.ColumnIndex > 3)
            {
                decimal result;
                if (this.dgv_data[e.ColumnIndex, e.RowIndex].Value == null)
                    return;
                if (!decimal.TryParse(this.dgv_data[e.ColumnIndex, e.RowIndex].Value.ToString(), out result))
                {
                    Dialog dialog = new Dialog();
                    dialog.Warning("数据格式错误");
                    this.dgv_data.CurrentCell = this.dgv_data[e.ColumnIndex, e.RowIndex];
                    //this.dgv_data[e.ColumnIndex, e.RowIndex].Selected = true;
                    this.dgv_data[e.ColumnIndex, e.RowIndex].Value = 0;
                }
            }
        }

        private void dgv_data_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dgv_data_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.dgv_data.ContextMenuStrip = this.contextMenuStrip1;
        }

        private void 删除此行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = this.dgv_data.SelectedRows;
            if (rows.Count > 0)
            {
                this.dgv_data.Rows.Remove(rows[0]);
            }

            this.dgv_data.ContextMenuStrip = null;
        }

        private void dgv_data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.dgv_data.ContextMenuStrip = null;
        }

        private void AxisPoint_FormClosing(object sender, FormClosingEventArgs e)
        {
            M_Closed();
        }
        public void M_Closed()
        {

        }
    }
}
